package com.yuncheng.oaApi.spcy;


import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.vo.*;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import org.apache.commons.collections.CollectionUtils;
import com.yuncheng.oaApi.utils.GetUuIdUtils;;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.oaApi.bxdService.OaJcyFybxdService;

import com.yuncheng.oaApi.config.OaMinioUtils;
import com.yuncheng.oaApi.constant.*;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.oaApi.utils.NumberToChineseCapital;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import com.yuncheng.spcyApi.alog.annotation.Log;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author hua
 */
@Api(tags = "检查员费用报销单管理")
@RestController
@RequestMapping("/api/spzx-oa/zzrybxd")
public class OaJcyFybxdController {

    @Resource
    @Lazy
    private OaJcyFybxdService oaJcyFybxdService;


    @Resource
    @Lazy
    private INkRsCcspService nkRsCcspService;

    @Resource
    @Lazy
    private IOaXzswspEntityService oaXzswspService;

    @Resource
    @Lazy
    private IOaBgypsqglService oaBgypsqglService;


    @Resource
    @Lazy
    private INkRsXmyszbbService nkRsXmyszbbService;

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private INkRsZzsqdService nkRsZzsqdService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxdService spcyJcyFybxdService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxdgrjlService spcyFybxdgrlService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxdtssxsqService spcyJcyFybxdtssxsqService;


    @Resource
    private IOaFjzykEntityService oaFjzykService;

    @Resource
    private OaMinioUtils oaMinioUtils;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    @Resource
    @Lazy
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private INkBxJtxcdService nkBxJtxcdService;

    @Resource
    @Lazy
    private INkRsHtspjlService nkRsHtspjlService;





    @Log(title = "未发起流程的报销列表")
    @ApiOperation(value = "未发起流程的报销列表")
    @GetMapping(value = "bxList")
    public HttpResult<?> queryFybxdPageLists( @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              OaSpcyJcyFybxd fybxd){
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();

        LambdaQueryWrapper<OaSpcyJcyFybxd> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(currentUser.getId())){


            objectLambdaQueryWrapper.eq(OaSpcyJcyFybxd::getFsffqlc, OaCommonFlowConstant.SFFQLC_NO);
            objectLambdaQueryWrapper.eq(StringUtils.isNotBlank(fybxd.getFdjh()), OaSpcyJcyFybxd::getFjbrid,fybxd.getFdjh());
            objectLambdaQueryWrapper.eq(StringUtils.isNotBlank(fybxd.getFjbr()), OaSpcyJcyFybxd::getFjbr,fybxd.getFjbr());
            objectLambdaQueryWrapper.eq(StringUtils.isNotBlank(fybxd.getFbxdlx()), OaSpcyJcyFybxd::getFbxdlx,fybxd.getFbxdlx());
            objectLambdaQueryWrapper.eq(OaSpcyJcyFybxd:: getFjbrid,currentUser.getId());

            objectLambdaQueryWrapper.orderByDesc(OaSpcyJcyFybxd::getCreateTime);

        }
        Page<OaSpcyJcyFybxd> objectPage = new Page<>(pageNo,pageSize);

        Page<OaSpcyJcyFybxd> page = spcyJcyFybxdService.page(objectPage, objectLambdaQueryWrapper);
        return HttpResult.ok(page);
    }

    @Log(title = "删除未发起流程的报销列表")
    @ApiOperation(value = "删除未发起流程的报销列表")
    @GetMapping(value = "deletbxList")
    public HttpResult deletbxList(String id){
        Arrays.asList(id.split("[,，]")).forEach(item->{
            spcyJcyFybxdService.removeById(item);
            //个人记录
            List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyFybxdgrlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>().eq(OaSpcyJcyFybxdgrjl::getFbxdid, item));
            spcyFybxdgrlService.removeBatchByIds(jcyFybxdgrjls);
            //特殊事项
            List<OaSpcyJcyFybxdtssxsq> oaSpcyJcyFybxdtssxsqs = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>().eq(OaSpcyJcyFybxdtssxsq::getFbxdid, item));
            spcyJcyFybxdtssxsqService.removeBatchByIds(oaSpcyJcyFybxdtssxsqs);
            //转账申请
            List<NkRsZzsqd> nkRsZzsqdList = nkRsZzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>().eq(NkRsZzsqd::getFbxdid, item));
            nkRsZzsqdService.removeBatchByIds(nkRsZzsqdList);
        });


        return HttpResult.ok("成功");

    }







    @Log(title = "查询费用报销单-分页列表")
    @ApiOperation(value = "查询费用报销单-分页列表")
    @GetMapping(value = "queryFybxdPageList")
    public HttpResult queryFybxdPageList(OaSpcyJcyFybxd fybxd, String gjz, PageVo pageVo, OrderItemVo orderItemVo){
        return oaJcyFybxdService.queryFybxdPageList(fybxd, gjz, false, pageVo, orderItemVo);
    }
    /**
     * 查询我的费用报销单-分页列表
     */
    @GetMapping(value = "queryMyFybxdPageList")
    public HttpResult queryMyFybxdPageList(OaSpcyJcyFybxd fybxd, String gjz, PageVo pageVo, OrderItemVo orderItemVo){
        return oaJcyFybxdService.queryFybxdPageList(fybxd, gjz, true, pageVo, orderItemVo);
    }

//    @Log(title = "初始化申请表单")
//    @GetMapping(value = "initSqsxFybxd")
//    public HttpResult initSqsxFybxd(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,String flcbm,String bxlx,String sfdgzz,String bxdlx) throws Exception {
//        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
//        if (StringUtils.isBlank(sqsxid) && StringUtils.isBlank(flcbm)) {
//            List<SpcyJcyFybxd> fybxds = spcyJcyFybxdService.list(new LambdaQueryWrapper<SpcyJcyFybxd>()
//                    .eq(SpcyJcyFybxd::getFjbrid, currentUser.getId())
//                    .eq(SpcyJcyFybxd::getFsffqlc, CommonFlowConstant.FSFFQLC_NO)
//                    .eq(SpcyJcyFybxd::getFbxlx, bxlx)
//                    .eq(SpcyJcyFybxd :: getFbxdlx,bxdlx));
//
//            if (CollectionUtils.isNotEmpty(fybxds)) {
//                SpcyJcyFybxd spcyJcyFybxd = fybxds.get(0);
//                return HttpResultVo.HttpResultList(spcyJcyFybxd);
//            } else {
//                Result result = jcyFybxdService.initSqsxFybxd(sqsxid, flcbm,bxlx,sfdgzz);
//                return HttpResultVo.HttpResultList(result.getResult());
//            }
//        }else {
//            List<SpcyJcyFybxd> fybxds = spcyJcyFybxdService.list(new LambdaQueryWrapper<SpcyJcyFybxd>()
//                    .eq(SpcyJcyFybxd::getFsqsxid, sqsxid)
//                    .eq(SpcyJcyFybxd::getFsffqlc, CommonFlowConstant.FSFFQLC_NO));
//
//            if (CollectionUtils.isNotEmpty(fybxds)){
//                SpcyJcyFybxd spcyJcyFybxd = fybxds.get(0);
//                return HttpResultVo.HttpResultList(spcyJcyFybxd);
//            }else {
//                Result result = jcyFybxdService.initSqsxFybxd(sqsxid, flcbm,bxlx,sfdgzz);
//                return HttpResultVo.HttpResultList(result.getResult());
//            }
//
//        }
//    }


    @Log(title = "从未报销打开申请表单")
    @GetMapping(value = "initSqsxFybxds")
    public HttpResult initSqsxFybxds(String id) throws Exception {
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(id);
        return HttpResultVo.HttpResultList(oaSpcyJcyFybxd);

    }


    @Log(title = "初始化申请表单")
    @GetMapping(value = "initSqsxFybxd")
    public HttpResult initSqsxFybxd(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,String flcbm,String bxlx,String sfdgzz,String bxdlx) throws Exception {
        Result result = oaJcyFybxdService.initSqsxFybxd(sqsxid, flcbm,bxlx,sfdgzz);
        return HttpResultVo.HttpResultList(result.getResult());

    }

    /**
     * 查询报销单列表-根据sqsxid
     */
    @GetMapping(value = "queryBxdListBySqsxId")
    public HttpResult queryBxdListBySqsxId(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,String lcbm) throws Exception {

        List<OaSpcyJcyFybxd> list = oaJcyFybxdService.queryBxdListBySqsxId(sqsxid,lcbm);

        return HttpResultVo.HttpResultList(list);
    }


    @Log(title = "查询报销人员信息列表-根据sqsxid")
    @ApiOperation(value = "查询报销人员信息列表-根据sqsxid")
    @GetMapping(value = "queryBxryxxListBySqsxId")
    public HttpResult queryBxryxxListBySqsxId(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,String lcbm,String bxdlx) throws Exception {


        return HttpResult.ok(oaJcyFybxdService.queryBxryxxListBySqsxId(sqsxid,lcbm));
    }



    @Log(title = "查询报销人员信息列表-根据单据号")
    @ApiOperation(value = "查询报销人员信息列表-根据单据号")
    @GetMapping(value = "queryBxryxxListByDjh")
    public HttpResult queryBxryxxListByDjh(String djh,String flcbm,String sqsxid) throws Exception {


        return HttpResult.ok(oaJcyFybxdService.queryBxryxxListByDjh(djh,flcbm,sqsxid));
    }

    /**
     * 删除费用报销单及明细
     * @param fbxdid
     */
    @GetMapping(value = "deleteFybxdAndRymx")
    public HttpResult deleteFybxdAndRymx(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        oaJcyFybxdService.deleteFybxdAndRymx(fbxdid);

        return HttpResult.ok("删除成功");
    }



    @Log(title = "查询报销人员信息列表-根据报销单id")
    @ApiOperation(value = "查询报销人员信息列表-根据报销单id")
    @GetMapping(value = "queryBxryxxListByBxdId")
    public HttpResult queryBxryxxListByBxdId(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){
        if (StringUtils.isBlank(fbxdid)){
            return HttpResultVo.HttpResultList("");
        }
        List<OaSpcyJcyFybxdgrjl> list = oaJcyFybxdService.queryBxryxxListByBxdId(fbxdid);


        return HttpResultVo.HttpResultList(list);
    }

    @Log(title = "查询报销单信息-根据报销单id")
    @ApiOperation(value = "查询报销单信息-根据报销单id")
    @GetMapping(value = "queryBxdById")
    public HttpResult queryBxdById(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){
        return HttpResult.ok(oaJcyFybxdService.queryBxdById(fbxdid));
    }

    @Log(title = "查询报销单及人员明细-根据报销单id")
    @ApiOperation(value = "查询报销单及人员明细-根据报销单id")
    @GetMapping(value = "queryBxdMxById")
    public HttpResult queryBxdMxById(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){
        Map resultMap = new HashMap();
        OaSpcyJcyFybxd fybxd = oaJcyFybxdService.queryBxdById(fbxdid);
        List<OaSpcyJcyFybxdgrjl> bxmxList = oaJcyFybxdService.queryBxryxxListByBxdId(fbxdid);

        resultMap.put("bxd", fybxd);
        resultMap.put("bxmxList", bxmxList);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "查询可选择人员-分页列表")
    @ApiOperation(value = "查询可选择人员-分页列表")
    @GetMapping(value = "queryKSelectUserPageList")
    public HttpResult queryKSelectUserPageList(YcSysUser query, String gjz, PageVo pageVo){
        return oaJcyFybxdService.queryKSelectUserPageList(query, gjz, pageVo);
    }

    @Log(title = "新增报销人员用户信息")
    @ApiOperation(value = "新增报销人员用户信息")
    @PostMapping(value = "addBxryUserInfo")
    public HttpResult addBxryUserInfo(@RequestBody AddJcyBxryInfoVo jcyBxryInfoVo) throws InvocationTargetException, IllegalAccessException {

        YcSysUser newUser = oaJcyFybxdService.addBxryUserInfo(jcyBxryInfoVo);

        return HttpResultVo.HttpResultSave(newUser,"添加成功");
    }

    @Log(title = "保存选择报销单-报销人员")
    @ApiOperation(value = "保存选择报销单-报销人员")
    @GetMapping(value = "saveSelectBxry")
    public HttpResult saveSelectBxry(@RequestParam(value = "ids",defaultValue = "")String ids,
                                     @RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                     @RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        oaJcyFybxdService.saveSelectBxry(ids, fbxdid, sqsxid);

        return HttpResult.ok("选择成功");
    }

    @Log(title = "删除报销人员明细-根据报销人员id")
    @ApiOperation(value = "删除报销人员明细-根据报销人员id")
    @DeleteMapping(value = "deleteBxrymxById")
    public HttpResult deleteBxrymxById(@RequestParam(value = "id",defaultValue = "")String id){

        oaJcyFybxdService.deleteBxrymxById(id);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "更新报销单信息")
    @ApiOperation(value = "更新报销单信息")
    @PostMapping(value = "updateBxd")
    public HttpResult updateBxd(@RequestBody HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {

        String id = buffer.get("id");
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdService.getById(id);

        //通过预算项目id 来判断是否是劳务费

        String fysxmid = buffer.get("fysxmid");
        if (StringUtils.isNotBlank(fysxmid) && oaSpcyJcyFybxd.getFbxlx().equals(OaJcyFybxdConstant.STATE_YBBX) && buffer.get("fbxdlx").equals(OaJcyFybxdConstant.FYBXD_ZCBXD)){
            List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyFybxdgrlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                    .eq(OaSpcyJcyFybxdgrjl::getFbxdid, id));
            OaSpcyJcyFybxdgrjl bxdGrjl = jcyFybxdgrjls.get(0);
            NkRsXmyszbb rsXmyszbb = nkRsXmyszbbService.getById(fysxmid);
            if (rsXmyszbb.getFbmjjflmc().equals(OaJcyFybxdConstant.BXXMFL_LWF)){
                //劳务费报销金额 是 一般报销金额
                bxdGrjl.setFlwfsbje(oaSpcyJcyFybxd.getFybbxje());

                //劳务税个税
                if(StringUtils.isNotBlank(oaSpcyJcyFybxd.getFybbxje())){
                    Map getsqje = oaJcyFybxdService.getsqje(oaSpcyJcyFybxd);
                    bxdGrjl.setFlwfgsje(String.valueOf(getsqje.get("tax"))); //个税
                    bxdGrjl.setFlwfsqje(String.valueOf(getsqje.get("preTaxAmount"))); //税前金额
                    spcyFybxdgrlService.updateById(bxdGrjl);
                }
            }
        }



        oaJcyFybxdService.updateBxd(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "更新报销人员明细信息")
    @ApiOperation(value = "更新报销人员明细信息")
    @PostMapping(value = "updateBxrymxxx")
    public HttpResult updateBxrymxxx(@RequestBody HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {

        oaJcyFybxdService.updateBxrymxxx(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "查询当前记录的对应垫付人员列表")
    @ApiOperation(value = "查询当前记录的对应垫付人员列表")
    @GetMapping(value = "queryFybxdDfryList")
    public HttpResult queryFybxdDfryList(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                         @RequestParam(value = "id",defaultValue = "")String id){

        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        List<Map> mapList = oaJcyFybxdService.queryFybxdDfryList(fbxdid, id);

        return HttpResult.ok(mapList);
    }

    @Log(title = "查询申请人员列表")
    @ApiOperation(value = "查询申请人员列表")
    @GetMapping(value = "queryFybxdSqryList")
    public HttpResult queryFybxdSqryList(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        if (StringUtils.isBlank(fbxdid)){
            throw new BusinessException("参数为空！");
        }
        List<Map> mapList = oaJcyFybxdService.queryFybxdDfryList(fbxdid, null);

        return HttpResultVo.HttpResultList(mapList);
    }

    @Log(title = "报销单信息-下一步")
    @ApiOperation(value = "报销单信息-下一步")
    @GetMapping(value = "submitFybxd")
    public HttpResult submitFybxd(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid) throws InvocationTargetException, IllegalAccessException {

        return oaJcyFybxdService.submitFybxd(fbxdid);
    }

    @Log(title = "报销单信息-上一步")
    @ApiOperation(value = "报销单信息-上一步")
    @GetMapping(value = "returnFybxd")
    public HttpResult returnFybxd(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        return oaJcyFybxdService.returnFybxd(fbxdid);
    }


    @Log(title = "报销单信息/对公或对私-下一步")
    @ApiOperation(value = "报销单信息/对公或对私-下一步")
    @GetMapping(value = "submitFybxdYbbx")
    public HttpResult submitFybxdYbbx(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid) throws InvocationTargetException, IllegalAccessException {

        return oaJcyFybxdService.submitFybxds(fbxdid);
    }

    @Log(title = "报销单信息/对公或对私-上一步")
    @ApiOperation(value = "报销单信息/对公或对私-上一步")
    @GetMapping(value = "returnFybxdYbbx")
    public HttpResult returnFybxdYbbx(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        return oaJcyFybxdService.returnFybxds(fbxdid);
    }




    @Log(title = "批量设置出行方式")
    @ApiOperation(value = "批量设置出行方式")
    @GetMapping(value = "batchSetBxdCxfs")
    public HttpResult batchSetBxdCxfs(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                      @RequestParam(value = "fcsjjtfs",defaultValue = "")String fcsjjtfs){

        oaJcyFybxdService.batchSetBxdCxfsByBxdId(fbxdid, fcsjjtfs);

        return HttpResult.ok("设置成功");
    }


    @Log(title = "申请依据列表查询")
    @ApiOperation(value = "申请依据列表查询")
    @GetMapping(value = "sqLitsSelect")
    public HttpResult sqLitsSelect(String fxm,String flcbm ,PageVo pageVo) {

        Page<NkRsCcsp> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<OaBgypsqgl> page2 = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<OaXzswspEntity> page3 = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        Page<NkRsHtspjl> page4 = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());

        if (flcbm.equals(OaFlowConstant.NK_CCSP)){
            List<NkRsCcsp> list = nkRsCcspService.list(new LambdaQueryWrapper<NkRsCcsp>().eq(NkRsCcsp::getFlczt, OaFlowNodeConstant.HJMC_LCJS)
                    .eq(StringUtils.isNotBlank(fxm),NkRsCcsp::getFxm , fxm)
                    .eq(NkRsCcsp :: getFbxzt,NkConstant.BXZT_WBX));
            //测试直接查
//            List<NkRsCcsp> list = nkRsCcspService.list(new LambdaQueryWrapper<NkRsCcsp>()
//                    .eq(StringUtils.isNotBlank(fxm),NkRsCcsp::getFxm , fxm));
            page.setRecords(list);
            return HttpResult.ok(page);
        }else if (flcbm.equals(OaFlowConstant.OA_BGYPSQ)){
            List<OaBgypsqgl> list = oaBgypsqglService.list(new LambdaQueryWrapper<OaBgypsqgl>().eq(OaBgypsqgl::getFlczt, OaFlowNodeConstant.HJMC_LCJS)
                     .eq(StringUtils.isNotBlank(fxm),OaBgypsqgl::getFlyr , fxm));
            page2.setRecords(list);
            return HttpResult.ok(page2);
        }else if (flcbm.equals(OaFlowConstant.OA_XZSWSP)){
            List<OaXzswspEntity> list = oaXzswspService.list(new LambdaQueryWrapper<OaXzswspEntity>().eq(OaXzswspEntity::getFlczt, OaFlowNodeConstant.HJMC_LCJS)
                    .like(StringUtils.isNotBlank(fxm), OaXzswspEntity::getFnbr , fxm)
                    .eq(OaXzswspEntity::getFlb,OaFlowConstant.XZSWLB_CW)
                    .eq(OaXzswspEntity::getFbxzt,NkConstant.BXZT_WBX));
            page3.setRecords(list);
            return HttpResult.ok(page3);
        }else if (flcbm.equals(OaFlowConstant.NK_HTSPLC)){
            List<NkRsHtspjl> list = nkRsHtspjlService.list(new LambdaQueryWrapper<NkRsHtspjl>().eq(NkRsHtspjl::getFlczt, OaFlowNodeConstant.HJMC_LCJS)
                    .like(StringUtils.isNotBlank(fxm), NkRsHtspjl::getFtxr , fxm)
                    .eq(NkRsHtspjl::getFbxzt,NkConstant.BXZT_WBX));
            page4.setRecords(list);
            return HttpResult.ok(page4);
        }

        throw new BusinessException("未查询到相关参数");
    }


    @Log(title = "特殊事项申请-分页查询")
    @ApiOperation(value = "特殊事项申请-分页查询")
    @GetMapping(value = "queryFybxdTssxSqPageList")
    public HttpResult queryFybxdTssxSqPageList(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                               PageVo pageVo){

        return oaJcyFybxdService.queryFybxdTssxSqPageList(fbxdid, pageVo);
    }


    @Log(title = "特殊事项申请-分页查询-使用申请事项id")
    @ApiOperation(value = "特殊事项申请-分页查询-使用申请事项id")
    @GetMapping(value = "queryFybxdTssxSqPageListBysqsxid")
    public HttpResult queryFybxdTssxSqPageListBysqsxid(String fsqsxid,
                                               PageVo pageVo){

        return oaJcyFybxdService.queryFybxdTssxSqPageListBysqsxid(fsqsxid, pageVo);
    }

    @Log(title = "特殊事项申请-分页查询-使用申请事项id")
    @ApiOperation(value = "特殊事项申请-分页查询-使用申请事项id")
    @GetMapping(value = "querytssxsqByfybxd")
    public HttpResult querytssxsqByfybxd(String fsqsxid,PageVo pageVo){

        Result result = oaJcyFybxdService.querytssxsqByfybxd(fsqsxid, pageVo);
        return HttpResultVo.HttpResultList(result.getResult());
    }




    @Log(title = "特殊事项申请-根据id查看")
    @ApiOperation(value = "特殊事项申请-根据id查看")
    @GetMapping(value = "queryFybxdTssxSqById")
    public HttpResult queryFybxdTssxSqById(@RequestParam(value = "id",defaultValue = "")String id){

        return HttpResult.ok(oaJcyFybxdService.queryFybxdTssxSqById(id));
    }

    @Log(title = "特殊事项申请-新增")
    @ApiOperation(value = "特殊事项申请-新增")
    @PostMapping(value = "addFybxdTssxSq")
    public HttpResult addFybxdTssxSq(@RequestBody OaSpcyJcyFybxdtssxsq bean){

        return HttpResultVo.HttpResultSave(oaJcyFybxdService.addFybxdTssxSq(bean), "添加成功");
    }

    @Log(title = "特殊事项申请-初始化")
    @ApiOperation(value = "特殊事项申请-初始化")
    @GetMapping(value = "initFybxdTssxSq")
    public HttpResult initFybxdTssxSq(String fsxsqid,String fsqlx,String lcbm) throws Exception {
        List<OaSpcyJcyFybxdtssxsq> oaSpcyJcyFybxdtssxsqs = spcyJcyFybxdtssxsqService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdtssxsq>()
                .eq(OaSpcyJcyFybxdtssxsq::getFsxsqid, fsxsqid)
                .eq(OaSpcyJcyFybxdtssxsq:: getFsqlx,fsqlx));
        if (CollectionUtils.isNotEmpty(oaSpcyJcyFybxdtssxsqs)){
            return HttpResultVo.HttpResultList(oaSpcyJcyFybxdtssxsqs.get(0));
        }
        Result result = oaJcyFybxdService.initFybxdTssxSq(fsxsqid, fsqlx, lcbm);
        return HttpResultVo.HttpResultList(result.getResult());
    }

    @Log(title = "更新特殊事项报销单id")
    @ApiOperation(value = "更新特殊事项报销单id")
    @PostMapping(value = "updateFybxdTssxSqid")
    public HttpResult updateFybxdTssxSqid(String fbxdid,String sxsqid){
        oaJcyFybxdService.updatetssqbxdid(fbxdid,sxsqid);
        return HttpResultVo.HttpResultList("添加成功");
    }



    @Log(title = "特殊事项申请-更新")
    @ApiOperation(value = "特殊事项申请-更新")
    @PutMapping(value = "updateFybxdTssxSq")
    public HttpResult updateFybxdTssxSq(@RequestBody HashMap<String,String> buffer) throws InvocationTargetException, IllegalAccessException {
        oaJcyFybxdService.updateFybxdTssxSq(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "特殊事项申请-删除")
    @ApiOperation(value = "特殊事项申请-删除")
    @GetMapping(value = "deleteFybxdTssxSq")
    public HttpResult deleteFybxdTssxSq(@RequestParam(value = "id",defaultValue = "")String id){
        oaJcyFybxdService.deleteFybxdTssxSq(id);

        return HttpResult.ok("删除成功");
    }


    @Log(title = "查询当前经办人的部门预算")
    @ApiOperation(value = "查询当前经办人的部门预算")
    @GetMapping(value = "selectBmysList")
    public HttpResult selectBmysList(NkRsXmyszbb nkRsXmyszbb,PageVo pageVo){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        String orgId = ycSysUserService.getUserZbmByUserId(currentUser.getId());
        List<NkRsXmyszbb> nkRsXmyszbbList = nkRsXmyszbbService.list(new LambdaQueryWrapper<NkRsXmyszbb>()
                .eq(NkRsXmyszbb::getFbmid, orgId)
                .eq(StringUtils.isNotBlank(nkRsXmyszbb.getFbmid()),NkRsXmyszbb::getFbmid,nkRsXmyszbb.getFbmid())
                .eq(StringUtils.isNotBlank(nkRsXmyszbb.getFbmjjflmc()),NkRsXmyszbb::getFbmjjflmc,nkRsXmyszbb.getFbmjjflmc())
                .eq(StringUtils.isNotBlank(nkRsXmyszbb.getFbmjjflbm()),NkRsXmyszbb::getFbmjjflbm,nkRsXmyszbb.getFbmjjflbm())
                .eq(StringUtils.isNotBlank(nkRsXmyszbb.getFysxmmc()),NkRsXmyszbb::getFysxmmc,nkRsXmyszbb.getFysxmmc()));

        Page<NkRsXmyszbb> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        page.setRecords(nkRsXmyszbbList);
        return HttpResult.ok(page);

    }

    @Log(title = "从申请依据更新报销单")
    @ApiOperation(value = "从申请依据更新报销单")
    @GetMapping(value = "updateJcyFybxdGrjl")
    public HttpResult updateJcyFybxdGrjl(String bxdid,String sqsxid,String flcbm) throws Exception {


        String sqsxids = oaJcyFybxdService.updateJcyFybxdGrjl(bxdid, sqsxid, flcbm);
        return HttpResultVo.HttpResultList(sqsxids);
    }


    @Log(title = "新增转账申请单")
    @ApiOperation(value = "新增转账申请单")
    @GetMapping(value = "addzzsq")
    public HttpResult addzzsq(NkRsZzsqd nkRsZzsqd){
        if (StringUtils.isNotBlank(nkRsZzsqd.getFjtxcdid())){
            NkBxJtxcd nkBxJtxcd = nkBxJtxcdService.getById(nkRsZzsqd.getFjtxcdid());
            nkBxJtxcd.setFsfzz(OaFlowConstant.YES);
            nkBxJtxcd.setFwjtfdfrxm(nkRsZzsqd.getFskdw());
            nkBxJtxcd.setFwjtfybje(nkRsZzsqd.getFjexx());
            nkBxJtxcd.setFwjtfsbje("0");
            nkBxJtxcd.setFwjtfsfdf(OaFlowConstant.YES);
            nkBxJtxcdService.updateById(nkBxJtxcd);
        }


        Result addzzsq = oaJcyFybxdService.addzzsq(nkRsZzsqd);


        return HttpResultVo.HttpResultList(addzzsq.getResult());
    }

    @Log(title = "查询转账申请单")
    @ApiOperation(value = "查询转账申请单")
    @GetMapping(value = "selectzzsq")
    public HttpResult selectzzsq(String fbxdid){
        Result result = oaJcyFybxdService.selectZzsqd(fbxdid);
        return HttpResultVo.HttpResultList(result.getResult());
    }


    @Log(title = "修改转账申请单")
    @ApiOperation(value = "修改转账申请单")
    @PutMapping(value = "updatezzsq")
    public HttpResult updatezzsq(@RequestBody NkRsZzsqd nkRsZzsqd){
        String fjexx = nkRsZzsqd.getFjexx();
        String fwfqk = nkRsZzsqd.getFwfqk();
        if (fwfqk.equals(NkConstant.CCWF_FJ) && StringUtils.isNotBlank(nkRsZzsqd.getFbxdgrjlid())){
            OaSpcyJcyFybxdgrjl fybxdgrjl = spcyFybxdgrlService.getById(nkRsZzsqd.getFbxdgrjlid());
            if (fwfqk.equals(NkConstant.CCWF_FJ)){
                fybxdgrjl.setFsfqc(NkConstant.SFYFK_YES);
            }
            spcyFybxdgrlService.updateById(fybxdgrjl);
        }


        nkRsZzsqd.setFjedx(NumberToChineseCapital.toChineseMoney(new BigDecimal(fjexx)));
        nkRsZzsqdService.updateById(nkRsZzsqd);
        NkRsZzsqd nkRsZzsqd1 = nkRsZzsqdService.getById(nkRsZzsqd.getId());
        updatejczt(nkRsZzsqd1.getFbxdid());
        return HttpResultVo.HttpResultList("更新成功");
    }

    @Log(title = "删除转账申请单")
    @ApiOperation(value = "删除转账申请单")
    @DeleteMapping(value = "Deletzzsq")
    public HttpResult Deletzzsq(String id){
        //删除转账申请单的同时，清空报销单的关于转账的字段
        NkRsZzsqd nkRsZzsqd = nkRsZzsqdService.getById(id);
        //需要拿到的数据，在删除之前拿取
        String fjtxcdid = nkRsZzsqd.getFjtxcdid(); //报销单id

        NkBxJtxcd nkBxJtxcd = nkBxJtxcdService.getById(fjtxcdid);
        if (nkBxJtxcd != null){
            nkBxJtxcd.setFsfzz(OaFlowConstant.NO);
            nkBxJtxcd.setFwjtfdfrxm("");
            nkBxJtxcd.setFwjtfybje("0");
            nkBxJtxcdService.updateById(nkBxJtxcd);
        }
        nkRsZzsqdService.removeById(id);

        return HttpResultVo.HttpResultList("删除成功");
    }



    @Log(title = "集合全部查询")
    @ApiOperation(value = "集合全部查询")
    @GetMapping(value = "getZzryBd")
    public HttpResult getZzryBd(String bxdid) {
        Map zzryBd = oaJcyFybxdService.getZzryBd(bxdid);
        return HttpResultVo.HttpResultList(zzryBd);
    }

    @Log(title = "更新出差补助/住宿费标准")
    @ApiOperation(value = "更新出差补助/住宿费标准")
    @GetMapping(value = "updateCcbz")
    public HttpResult updateCcbz(String bxdid) {
        oaJcyFybxdService.updateCcbzjqt(bxdid);
        return HttpResultVo.HttpResultList("更新成功");
    }


    @Log(title = "更新个人金额记录")
    @ApiOperation(value = "更新个人金额记录")
    @GetMapping(value = "updategrje")
    public HttpResult initSqsxFybxd(String bxdid) {
        oaJcyFybxdService.updategrje(bxdid);
        return HttpResultVo.HttpResultList("更新成功");
    }


    @Log(title = "根据报销明细 更新报销单")
    @ApiOperation(value = "根据报销明细 更新报销单")
    @GetMapping(value = "gethzdByBxd")
    public HttpResult gethzdByBxd(String bxdid) {
        oaJcyFybxdService.gethzdByBxd(bxdid);
        return HttpResultVo.HttpResultList("更新成功");
    }

    @Log(title = "更新交通费转账申请之后当前人的交通费状态")
    @ApiOperation(value = "更新交通费转账申请之后当前人的交通费状态")
    @GetMapping(value = "updatejczt")
    public HttpResult updatejczt(String fbxdid) {
        oaJcyFybxdService.updatejczt(fbxdid);
        return HttpResultVo.HttpResultList("更新成功");
    }



    @Log(title = "预览数据获取")
    @ApiOperation(value = "预览数据获取")
    @GetMapping(value = "getYlsj")
    public HttpResult getYlsj(String bxdid,String sqsxid) {
        Map ylsj = oaJcyFybxdService.getYlsj(bxdid, sqsxid);
        return HttpResultVo.HttpResultList(ylsj);
    }

    @Log(title = "清除申请依据")
    @ApiOperation(value = "清除申请依据")
    @GetMapping(value = "deletesqyj")
    public HttpResult deletesqyj(String bxdid) {
        oaJcyFybxdService.deletesqyj(bxdid);
        return HttpResultVo.HttpResultList("成功");
    }

    @AutoLog(value = "预览附件")
    @ApiOperation(value = "预览附件")
    @GetMapping(value = "Previewfj")
    public HttpResult Previewfj() {
        Map resultMap = new HashMap();
        String url = ""; // 查询文件全路径
        List<OaFjzykEntity> fjzyks = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFmc, OaJcyFybxdConstant.ZSF_BZ_FILE_NAME));

        OaFjzykEntity fjList = oaFjzykService.getById(fjzyks.get(0).getId());
        String fwjlj = fjList.getFwjlj();

        url = oaMinioUtils.preview(fwjlj);
        resultMap.put("url", url);
        return HttpResult.ok(resultMap);
    }

    @AutoLog(value = "minio-下载附件")
    @ApiOperation(value = "minio-下载附件")
    @GetMapping(value = "downLoadOa")
    public void downLoadOa(HttpServletResponse response) {
        List<OaFjzykEntity> fjzyks = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFmc, OaJcyFybxdConstant.ZSF_BZ_FILE_NAME));
        String id1 = fjzyks.get(0).getId();
        OaFjzykEntity fjList = oaFjzykService.getById(id1);
        String fwjlj = fjList.getFwjlj();
        if (StringUtils.isBlank(fwjlj)) {
            throw new BusinessException("当前附件记录不存在！");
        }
        oaMinioUtils.download(fwjlj, response);
    }


    @AutoLog(value = "相关制度查询修改菜单")
    @ApiOperation(value = "相关制度查询修改菜单")
    @GetMapping(value = "SystemMenu")
    public HttpResult SystemMenu(OaFjzykEntity oaFjzykEntity, PageVo pageVo) {
        List<OaFjzykEntity> oaFjzykEntities = oaFjzykService.list(new LambdaQueryWrapper<OaFjzykEntity>()
                .eq(OaFjzykEntity::getFlx, OaJcyFybxdConstant.ZSF_BZ_FILE_NAME_LX)
                .eq(StringUtils.isNotBlank(oaFjzykEntity.getFmc()), OaFjzykEntity::getFmc, oaFjzykEntity.getFmc()));

        Page<OaFjzykEntity> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        page.setRecords(oaFjzykEntities);
        return HttpResult.ok(page);
    }


    @PostMapping(value = "/fjupload")
    public HttpResult fjupload(MultipartFile file) {
        OaFjzykEntity oaFjzykEntity = new OaFjzykEntity();

        String id = "";
        if (file == null) {
            return HttpResult.error("上传失败,请先选择文件");
        }


        String originalFilename = file.getOriginalFilename();
        // 文件类型
        String wjlx = originalFilename.substring(originalFilename.lastIndexOf("."));

        // 文件名
        String sjmc = GetUuIdUtils.generateUuid();

        String wjccmc = sjmc + wjlx;

        String nowDay = DateConversionUtils.DateToMonth(new Date()); // 获取当前日期 yyyy-MM-dd

        //如果不放置文件， 在使用minio上传自定义文件夹时，文件要上传 ，在上传时 会直接上传 日期作为文件
        //附件存放地址-带文件
        String newDirs = OaCommonFlowConstant.OA_FILE_FJ + "/" + nowDay + "/" + wjccmc;

        String fileName = file.getOriginalFilename(); //带后缀的真实名称
        //上传文件
        oaMinioUtils.uploadFile(file, newDirs);

        //个人信息
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        id = GetUuIdUtils.ReplaceUuId();
        oaFjzykEntity.setFmc(fileName); //文件真实名称
        oaFjzykEntity.setFwjmc(wjccmc); //虚假名称
        oaFjzykEntity.setId(id);
        oaFjzykEntity.setFwjlx(wjlx);
        oaFjzykEntity.setFwjlj(newDirs);
        oaFjzykEntity.setFscr(currentUser.getName());
        oaFjzykEntity.setFscrid(currentUser.getId());
        oaFjzykEntity.setFscsj(DateConversionUtils.DateToYMDHMSChineseString(new Date()));
        oaFjzykEntity.setFlx(OaJcyFybxdConstant.ZSF_BZ_FILE_NAME_LX);
        oaFjzykService.save(oaFjzykEntity);
        if (StringUtils.isNotBlank(id)) {
            String czContent = "上传附件文档:";
            Map<String, Object> wjjlMap = ObjectToMapUtils.objectToMap(oaFjzykEntity);
            czContent += LogUtils.MapToStringMethod(wjjlMap);
            spcyLogService.addLogs(OaLogConstant.LOGS_OA_FJ, czContent, id, OaCommonFlowConstant.LOGS_SYS_TYPE_BC, OaCommonFlowConstant.LOGS_SYS_TYPE_BC);

        }
        return HttpResult.ok("上传成功");
    }


    /**
     * 初始化提交还是退回后提交
     */
    @AutoLog(value = "初始化提交还是退回后提交")
    @ApiOperation(value = "初始化提交还是退回后提交")
    @GetMapping(value = "sftj")
    public HttpResult sftj(String bxdid){
        List<OaJobEntity> oaJobEntities = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>()
                .eq(OaJobEntity::getFpid, bxdid));
        if (CollectionUtils.isNotEmpty(oaJobEntities)){
            //如果不为空，代表已经生成过流程，那就是提交 为true
            return HttpResult.ok(true);

        } else {
            return HttpResult.ok(false);
        }
    }



    /**
     * 获取转账申请单。如果当前报销单有多个申请单 将收款单位相同的的金额相加并返回
     */

    @Log(title = "获取转账申请单列表")
    @ApiOperation(value = "获取转账申请单列表")
    @GetMapping(value = "jzsqd")
    public HttpResult jzsqd(String bxdid)  {
        //收款单位是定死的 只有两家
        List<NkRsZzsqd> nkRsZzsqds = nkRsZzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
                .eq(NkRsZzsqd::getFbxdid, bxdid));

        if (CollectionUtils.isNotEmpty(nkRsZzsqds)){
            BigDecimal nyhkbigDecimal = new BigDecimal(0);
            BigDecimal ffhkbigDecimal = new BigDecimal(0);

            Map nyhkmap = new HashMap();
            Map ffhkmap = new HashMap();

            ArrayList list = new ArrayList();
            ArrayList ytlist1 = new ArrayList();
            ArrayList ytlist2 = new ArrayList();


            for (NkRsZzsqd nkRsZzsqd:nkRsZzsqds){
                String fskdw = nkRsZzsqd.getFskdw();
                if (fskdw.equals(OaSpcyJcyFybxdConstant.FJ_SKDW_NYHKFWYXGW)){

                    nyhkbigDecimal = nyhkbigDecimal.add(new BigDecimal(nkRsZzsqd.getFjexx()));
//                    Map nyhkmap = new HashMap();
                    ytlist1.add(nkRsZzsqd.getFyt());
                    nyhkmap.put("fskdw", nkRsZzsqd.getFskdw());
                    nyhkmap.put("fzh",nkRsZzsqd.getFzh());
                    nyhkmap.put("fkhyh",nkRsZzsqd.getFkhyh());
                    nyhkmap.put("ffkfs",nkRsZzsqd.getFfkfs());
                    nyhkmap.put("fyt",ytlist1);
                    nyhkmap.put("fjexx", nyhkbigDecimal);
                    nyhkmap.put("fjedx",NumberToChineseCapital.toChineseMoney(nyhkbigDecimal));

                }

                if (fskdw.equals(OaSpcyJcyFybxdConstant.FJ_SKDW_FFSWFWYXGS)){

                    ffhkbigDecimal = ffhkbigDecimal.add(new BigDecimal(nkRsZzsqd.getFjexx()));
//                    Map ffhkmap = new HashMap();
                    ytlist2.add(nkRsZzsqd.getFyt());
                    ffhkmap.put("fskdw", nkRsZzsqd.getFskdw());
                    ffhkmap.put("fzh",nkRsZzsqd.getFzh());
                    ffhkmap.put("fkhyh",nkRsZzsqd.getFkhyh());
                    ffhkmap.put("ffkfs",nkRsZzsqd.getFfkfs());
                    ffhkmap.put("fyt",ytlist2);

                    ffhkmap.put("fjexx", ffhkbigDecimal);
                    ffhkmap.put("fjedx",NumberToChineseCapital.toChineseMoney(ffhkbigDecimal));

                }
            }
            if(!nyhkmap.isEmpty()){
                list.add(nyhkmap);
            }

            if (!ffhkmap.isEmpty()){
                list.add(ffhkmap);
            }

            return HttpResult.ok(list);
        }
        return HttpResult.ok();
    }


    /**
     * 一般报销删除申请依据
     */

    @Log(title = "一般报销删除申请依据")
    @ApiOperation(value = "一般报销删除申请依据")
    @GetMapping(value = "ybbxsc")
    public HttpResult ybbxsc(String id,String fsqsxid)  {
        List<String> list = Arrays.asList(fsqsxid.split(",|，"));

        for (String idlist:list){
            NkRsHtspjl nkRsHtspjl = nkRsHtspjlService.getById(idlist);
            if (nkRsHtspjl != null){
                nkRsHtspjl.setFbxzt(NkConstant.BXZT_WBX);
                nkRsHtspjlService.updateById(nkRsHtspjl);
            }
            OaXzswspEntity xzswspEntity = oaXzswspService.getById(idlist);
            if (xzswspEntity != null){
                xzswspEntity.setFbxzt(NkConstant.BXZT_WBX);
                oaXzswspService.updateById(xzswspEntity);
            }
        }

        OaSpcyJcyFybxd fybxd = spcyJcyFybxdService.getById(id);
        fybxd.setFsqsxid("");
        fybxd.setFsqyj("");
        fybxd.setFjcsxmc("");
        spcyJcyFybxdService.updateById(fybxd);

        return HttpResult.ok("成功");
    }


    /**
     * 一般报销 申请报销表头
     */
    @Log(title = "一般报销删除申请依据")
    @ApiOperation(value = "一般报销删除申请依据")
    @GetMapping(value = "getybbxbt")
    public HttpResult getybbxbt(String yszbid,String bxdid)  {
        Map map = new HashMap();

        NkRsXmyszbb nkRsXmyszbb = nkRsXmyszbbService.getById(yszbid);
        OaSpcyJcyFybxd fybxd = spcyJcyFybxdService.getById(bxdid);
        if (nkRsXmyszbb != null){
            map.put("xmmc",nkRsXmyszbb.getFysxmmc());
            map.put("jjkm",nkRsXmyszbb.getFbmjjflmc());
            map.put("je", fybxd.getFybbxje());
            return HttpResult.ok(map);
        }

        return HttpResult.ok();
    }






}
